//
//  KSPickerView.m
//  DecxForward
//
//  Created by 程强 on 16/10/27.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KSPickerView.h"

@interface KSPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) UILabel *titleLbl;
@end

@implementation KSPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setFrame:self.bounds];
        [self addSubview:backBtn];
        backBtn.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.6];
        [backBtn addTarget:self action:@selector(cancelChose) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 300)];
        [self addSubview:contentView];
        contentView.backgroundColor = [UIColor whiteColor];
        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, frame.size.width, 260)];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
        [contentView addSubview:_pickerView];
        self.components = 1;
        
        UIToolbar *bar = [[UIToolbar alloc]initWithFrame:CGRectMake(15, 0, frame.size.width-30, 40)];
        bar.barTintColor = [UIColor whiteColor];
        
        UIImage *cancelImage = [LoadImage(@"toolbar_close") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage *okImage = [LoadImage(@"toolbar_ok") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithImage:cancelImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelChose)];
        
        UIBarButtonItem *confirmItem = [[UIBarButtonItem alloc]initWithImage:okImage style:UIBarButtonItemStylePlain target:self action:@selector(confirmChose)];
        UIBarButtonItem *fixSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        bar.items = @[cancelItem,fixSpace,confirmItem];
        [contentView addSubview:bar];
        self.alpha = 0;
        
        self.titleLbl = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, frame.size.width-110, 30)];
        [bar addSubview:_titleLbl];
        _titleLbl.font = [UIFont systemFontOfSize:15.0];
        _titleLbl.textAlignment = NSTextAlignmentCenter;        
        
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 1.0;
            contentView.frame = CGRectMake(0, frame.size.height-300, frame.size.width, 300);
        }];
    }
    return self;
}
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLbl.text = title;
}
- (void)cancelChose
{
    [self removeFromSuperview];
}
- (void)confirmChose
{
    for (int i = 0; i<self.components; i++) {
        NSInteger row = [self.pickerView selectedRowInComponent:i];
        if (self.delegate && [self.delegate respondsToSelector:@selector(ksPickView:didConfirmRow:inComponent:)]) {
            [self.delegate ksPickView:self didConfirmRow:row inComponent:i];
        }
    }
    [self cancelChose];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.components;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.delegate ksPickerView:self numberOfRowsInComponent:component];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44.0;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.delegate ksPickerView:self titleForRow:row forComponent:component];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(ksPickerView:didSelectRow:inComponent:)]) {
        [self.delegate ksPickerView:self didSelectRow:row inComponent:component];
    }
}
- (void)reloadData
{
    [self.pickerView reloadAllComponents];
}
@end
